#include <QtTest>

#include <QDebug>
#include <QMetaType>

#include "Az.h"
#include "TestRecord.h"
#include "AzXmlDataSource.h"
#include "AzRootRecord.h"

const static int recSize = 4;
const static int childRecSize = 5;

TestRecord::TestRecord(QObject* parent):QObject(parent)  {

}


void TestRecord::initTestCase() {

    mRootRec = new AzRootRecord();

    AzRootRecord rootRec;


    AzField f;
    f.setDisplayName("Строка");
    f.setType(QVariant::String);
    rootRec.fields().append("String",f);
    f.setDisplayName("Целое");
    f.setType(QVariant::Int);
    rootRec.fields().append("int", f);
    f.setFieldKind(AzField::CalcFieldKind);
    f.setDisplayName("Стайт");
    f.setType(QVariant::String);
    rootRec.fields().append("RecState", f);

    *mRootRec = rootRec;
}


void TestRecord::cleanupTestCase() {
    delete mRootRec;
}

void TestRecord::insert() {

	try {
		for (int i = 0; i < recSize; ++i) {
            AzRecord* rec =  mRootRec->append();
            rec->setData(0,QString("Text record %1").arg(i));
            rec->setData(1,i);
            if (i != 1)
                rec->setRecordState(Az::Unmodified);
            if ( i == 2)
                rec->setData(0,"MOdify string");
            if ( i == 3)
                rec->setRecordState(Az::Deleted);

            if (i == 0) {
                for (int j = 0 ; j < childRecSize;++j) {
                    AzRecord* child = rec->append();
                    child->setData(0,QString("Children %1").arg(j));
                    child->setData(1,j);
                    child->setRecordState(Az::Unmodified);
                    for (int k = 0; k < childRecSize;++k) {
                        AzRecord* child2 = child->append();
                        child2->setData(0,QString("Children level2 %1").arg(j));
                        child2->setData(1,j);
                        child2->setRecordState(Az::Unmodified);
                        if (j == 0 )
                            child2->setRecordState(Az::Inserted);
                    }
                }
            }
        }
	}
		catch (const AzError &e) {
			qDebug() << e.message();

		}
	
    QCOMPARE(mRootRec->childCount(), recSize);



    AzXmlDataSource dataSource;
    AzAbstractDataSource *ds = &dataSource;
    QFile file("Inserted.xml");
    file.open(QIODevice::WriteOnly);
    dataSource.setDevice(&file);
    dataSource.setName("Test");
    dataSource.setDataPacketFlags(Az::DataPacket | Az::DeltaPacket  | Az::MetadataPacket);
	try {
        qDebug() << ds->writeData(mRootRec,0);
	}
    catch (const AzDataSourceException& e) {
        qDebug() << e.message() << e.nativeErrorMessage();
	}
}


void TestRecord::deleteAll() {
    mRootRec->deleteAll();
    AzXmlDataSource ds;
    QFile file("Deleted.xml");
    file.open(QIODevice::WriteOnly);
    ds.setDevice(&file);
    ds.setDataPacketFlags(Az::DataPacket | Az::DeltaPacket  | Az::MetadataPacket);
    ds.writeData(mRootRec,0);
    QCOMPARE(mRootRec->childCount(), 0);
}

void TestRecord::read() {
    AzXmlDataSource ds;
    QFile file("Inserted.xml");
    file.open(QIODevice::ReadOnly);
    ds.setDevice(&file);
    ds.setDataPacketFlags(Az::DataPacket | Az::DeltaPacket  | Az::MetadataPacket);
	try {
       AzRootRecord *rec = new AzRootRecord;
       ds.readData(rec,0,-1);
       qDebug() << rec->fields().size();
	}
    catch (const AzDataSourceException& e) {
        qDebug() << e.nativeErrorMessage();
	}

    QCOMPARE(mRootRec->childCount(), recSize);
}

void TestRecord::writeReaded() {
   //QFile file("RecWriteReaded.xml");
   // file.open(QIODevice::ReadWrite | QIODevice::Truncate | QIODevice::Unbuffered);
  //  mRootRec->dataSource()->write(&file);

}

//QTEST_MAIN(TestRecord)

//#include "TestRecord.moc"


